<template>
  <view class="container">
    <block v-if="isLoading">
      <u-skeleton rows="20" title :loading="isLoading"></u-skeleton>
    </block>
    <block v-if="!isLoading">
      <Header
        :isFirstPage="isFirstPage"
        :voteId="formData.voteId"
        :communityId="formData.communityId"
        :recordId="isVotingInfo.recordId"
        :VotingratioInfo="VotingratioInfo"
        :isVotingInfo="isVotingInfo"
        :scrollTop="scrollTop"
        :voteInfo="voteInfo"
      />
      <view class="content-section">
        <view class="content-box-item">
          <VoteCard
            :voteInfo="voteInfo"
            :formData="formData"
            :voteContent="voteInfo.voteContent"
            :isVotingInfo="isVotingInfo"
            :VotingratioInfo="VotingratioInfo"
          />
          <InitProgress :voteInfo="voteInfo" />
          <block v-if="houseList && houseList.length > 0">
          <Signature
            title="1、签名"
            :isVotingInfo="isVotingInfo"
            v-if="isSignature"
            @uploadSign="uploadSign"
            @goToSign="goToSign"
          />
          </block>
          <NotBinding   v-if="!houseList"  />
          <FileList :fileUrl="fileUrl" :isVotingInfo="isVotingInfo" />
        </view>
      </view>

      <BottomCard
        :isVotingInfo="isVotingInfo"
        @click="handleClick"
        :isClick="isClick"
        :voteInfo="voteInfo"
        :type="options.type"
      />
    </block>
     <VoteSuccessModal 
      ref="voteSuccessModal"
      :voteName="voteInfo.voteTitle"
   
      @close="onVoteSuccessClose"
    />
  </view>
</template>

<script>
import VoteMixin from '@/mixins/VoteMixin'
import Header from './components/Header.vue'
import VoteCard from './components/VoteFromCard.vue'
import BottomCard from '@/components/BottomCard/index.vue'
import Signature from '@/components/Signature/index.vue'
import { parseAttachmentList } from '@/util/function'
import FileList from './components/FileList.vue'
import InitProgress from './components/InitProgress.vue'
import { mapActions } from 'vuex'
import VoteSuccessModal from '@/components/VoteSuccessModal/index.vue'
import NotBinding from '@/components/NotBinding/index.vue'

export default {
  name: 'InitiativePage',
  mixins: [VoteMixin],
  components: {
    Header,
    VoteCard,
    BottomCard,
    Signature,
    FileList,
    InitProgress,
    VoteSuccessModal,
    NotBinding
  },
  
  data() {
    return {
      // 扩展mixin中的数据
      fileUrl: []
    }
  },
  methods: {
    ...mapActions("community", ["checkLoginAndBoundHouses"]),

    // 重写updateIsClick方法以适应initiative页面的特殊逻辑
    updateIsClick() {
      let fromdata = this.formData
      if (fromdata.voteSign) {
        this.isClick = true
      } else {
        this.isClick = false
      }
    },

    // 重写handleClick方法以适应initiative页面的特殊逻辑
    handleClick() {
      let fromdata = JSON.parse(JSON.stringify(this.formData))
      fromdata.optionList = this.formData.optionId
      fromdata.optionId = undefined
      fromdata.voteId = Number(fromdata.voteId)
      
      if (this.houseList && this.houseList.length > 0) {
        fromdata.houseId = this.houseList[0].houseId
      } else {
        uni.showModal({
          title: "提示",
          content: "您还没有绑定房屋，请先绑定房屋",
          success: () => {
            uni.switchTab({
              url: "/pages/home/index"
            })
          }
        })
        return
      }

      this.formData.houseId = ""
      this.formData.voteSign = ""
      this.formData.optionId = ""
      fromdata.optionList = []
      
      // 调用mixin中的voteAdd方法
       this.handleVoteAdd(fromdata)
    },

    // 重写handleAdditionalData方法来处理initiative页面特有的数据
    handleAdditionalData(data) {
      // 处理文件URL
      this.voteInfo.sumSignNum = data.sumSignNum
      this.fileUrl = parseAttachmentList(data.pmsVote.fileUrl)
    }
  },

  onShareAppMessage(res) {
    return this.share()
  }
}
</script>

<style lang="scss">
page {
  width: 100%;
  height: 100%;
}
.container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.content-section {
  width: 100%;
  background: linear-gradient(180deg, #eaf2fb 0%, #f6f9fc 100%);
  padding: 0 26rpx;
  padding-bottom: 230rpx;
  flex:1
}
</style> 